﻿using Mecha.EdgeCompute.Framework;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EdgeCompute
{
    public partial class Form1 : Form
    {
        ControlableThread controlableThread = null;
        public Form1()
        {
            InitializeComponent();
            controlableThread = new ControlableThread(null, null, null, 300000000, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(_DisplayChannel))
            {
                IsBackground = true
            };
            thread1.Start();


        }

        private void _DisplayChannel()
        {
            controlableThread.Start();

            if (this.InvokeRequired)
            {
                MethodInvoker me = delegate ()
                {
                    label2.Text = controlableThread.ThreadRunning.ToString();
                };
                this.Invoke(me);
            }
            else
            {
                label2.Text = controlableThread.ThreadRunning.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(fun_stop))
            {
                IsBackground = true
            };
            thread1.Start();

        }

        private void fun_stop()
        {
            controlableThread.Stop();
            if (this.InvokeRequired)
            {
                MethodInvoker me = delegate ()
                {
                    label2.Text = controlableThread.ThreadRunning.ToString();
                };
                this.Invoke(me);
            }
            else
            {
                label2.Text = controlableThread.ThreadRunning.ToString();
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {

            object obj = new object();

            LoppingProcedureHandler loppingProcedure = new LoppingProcedureHandler(SayHello);
            loppingProcedure.Invoke(obj);
        }
        public void SayHello(object obj)
        {
            Console.WriteLine("Hello Delegate!");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(_DisplayData))
            {
                IsBackground = true
            };
            thread1.Start();
        }

        private void _DisplayData()
        {
            while (true)
            {
                if (InvokeRequired)
                {
                    var list = ControlableThread.GetDataMap();

                    if (list.Count > 0)
                    {
                        MethodInvoker me = delegate ()
                        {
                            foreach (var a in list)
                            {
                                label1.Text = a;
                            }

                        };
                        BeginInvoke(me, null);
                    }
                }

                Thread.Sleep(2000);
            }


        }

        private void button5_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(fun_Pause))
            {
                IsBackground = true
            };
            thread1.Start();
        }

        private void fun_Pause()
        {
            controlableThread.Pause();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(fun_Resume))
            {
                IsBackground = true
            };
            thread1.Start();
        }

        private void fun_Resume()
        {
            controlableThread.Resume();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(fun_Fun17))
            {
                IsBackground = true
            };
            thread1.Start();

        }

        private void fun_Fun17()
        {
            while (true)
            {

                if (this.InvokeRequired)
                {
                    MethodInvoker me = delegate ()
                    {
                        label1.Text = DateTime.Now.ToString();
                    };
                    this.Invoke(me);
                }
                else
                {
                    label1.Text = DateTime.Now.ToString();
                }

                Thread.Sleep(1000);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Form2 form1 = new Form2();
            form1.ShowDialog();
        }
    }
}
